<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\BatchUploadPhotoForm;

use App\Admin\Repositories\Photo;
use App\Models\Product;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class PhotoController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new Photo(), function (Grid $grid) {
            $grid->column('id')->bold();
            $grid->column('img','相片')->image('',50,50);
            $grid->column('cup','罩杯')->select([
                ''=>'','B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F','G'=>'G','H'=>'H'
            ]);
            $grid->column('hairstyle','頭髮')->select(Product::HAIRSTYLE);

            $grid->column('name_maps_position')->select([
                0=>'待設置',1=>'上左',2=>'上右',3=>'中左',4=>'中右',5=>'下左',6=>'下右'
            ]);
            $grid->column('three_maps_position')->select([
                0=>'待設置',1=>'上左',2=>'上右',3=>'中左',4=>'中右',5=>'下左',6=>'下右'
            ]);
            $grid->column('video','視頻')->link(function (){
                if ($this->video){
                    return asset_upload($this->video);
                }
            });

            $grid->column('status')->switch();
            $grid->column('created_at');

            $modal = Modal::make()
                ->lg()
                ->title('批量导入')
                ->body(BatchUploadPhotoForm::make())
                ->button('<div class="pull-right" style="margin-left: 20px"><button class="btn btn-primary">批量导入</button></div>');

            $grid->tools($modal);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Photo(), function (Show $show) {
            $show->field('id');
            $show->field('img');
            $show->field('atlas');
            $show->field('name_maps_position');
            $show->field('three_maps_position');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Photo(), function (Form $form) {

            $form->image('img','相片')->autoUpload()->uniqueName()->move('photo')->retainable()->required();
            $form->radio('cup','罩杯')->options([
                'B'=>'B','C'=>'C','D'=>'D','E'=>'E','F'=>'F','G'=>'G','H'=>'H'
            ])->required()->default('B');
            $form->multipleImage('atlas')->autoUpload()->name(function ($file) {
                $name = md5(uniqid().rand(1,99999999));
                return $name.'.'.$file->guessExtension();
            })->move('photo')->saving(function ($atlas){
                return $atlas;
            });
            $form->file('video','视频')->autoUpload()->uniqueName()->move('video');
            $form->radio('hairstyle','頭髮')->options(Product::HAIRSTYLE)->default(1);
            $form->radio('name_maps_position')->options([
                0=>'待設置',1=>'上左',2=>'上右',3=>'中左',4=>'中右',5=>'下左',6=>'下右'
            ])->default(1);
            $form->radio('three_maps_position')->options([
                0=>'待設置',1=>'上左',2=>'上右',3=>'中左',4=>'中右',5=>'下左',6=>'下右'
            ])->default(6);
            $form->switch('status')->default(1);



        });
    }
}
